/**    
 * 文件名：JavaCvHistogramMatch.java    
 *    
 * 版本信息：    
 * 日期：2014年3月12日    
 * xyj 足下 xyj 2014     
 * 版权所有    
 *    
 */

import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_8U;
import static com.googlecode.javacv.cpp.opencv_core.cvGetSize;
import static com.googlecode.javacv.cpp.opencv_highgui.CV_LOAD_IMAGE_COLOR;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_COMP_BHATTACHARYYA;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_HIST_ARRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_RGB2GRAY;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCalcHist;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCompareHist;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCreateHist;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvCvtColor;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvNormalizeHist;
import static opencvtest.OpenCVUtils.loadAndShowOrExit;
import static opencvtest.OpenCVUtils.show;

import java.awt.image.BufferedImage;
import java.io.File;

import opencvtest.chapter04.Histogram1D;

import com.googlecode.javacv.cpp.opencv_core.IplImage;
import com.googlecode.javacv.cpp.opencv_imgproc.CvHistogram;

/**
 * @项目名称：opencv-test
 * @类名称：JavaCvHistogramMatch
 * @类描述：
 * @创建人：zhuyi
 * @创建时间：2014年3月12日 下午5:40:28
 * @修改人：zhuyi
 * @修改时间：2014年3月12日 下午5:40:28
 * @修改备注：
 * @version
 * 
 */
public class JavaCvHistogramMatch {

    public static void main(String[] args) {

        IplImage image = loadAndShowOrExit(new File("E:/image-test/z1.jpg"), CV_LOAD_IMAGE_COLOR);
        IplImage image2 = loadAndShowOrExit(new File("E:/image-test/z2.jpg"), CV_LOAD_IMAGE_COLOR);

        int histSize = 256;
        float[] range = { 0, 255 };
        float[][] ranges = { range };

        IplImage grayPlane = IplImage.create(cvGetSize(image), IPL_DEPTH_8U, 1);
        cvCvtColor(image, grayPlane, CV_RGB2GRAY);
        CvHistogram grayHist = cvCreateHist(1, new int[] { histSize }, CV_HIST_ARRAY, ranges, 1);

        cvCalcHist(new IplImage[] { grayPlane }, grayHist, 0, null);
        cvNormalizeHist(grayHist, 1);
        BufferedImage grayHistImg = new Histogram1D().getHistogram(grayHist);

        show(grayHistImg, "gray hist img 1");

        IplImage grayPlane2 = IplImage.create(cvGetSize(image2), IPL_DEPTH_8U, 1);
        cvCvtColor(image2, grayPlane2, CV_RGB2GRAY);
        CvHistogram grayHist2 = cvCreateHist(1, new int[] { histSize }, CV_HIST_ARRAY, ranges, 1);

        cvCalcHist(new IplImage[] { grayPlane2 }, grayHist2, 0, null);
        cvNormalizeHist(grayHist2, 1);
        BufferedImage grayHistImg2 = new Histogram1D().getHistogram(grayHist2);

        show(grayHistImg2, "gray hist img 2");

        double com = cvCompareHist(grayHist, grayHist2, CV_COMP_BHATTACHARYYA);
        System.out.println(com);
    }

}
